package cn.zjh.school.controller;

import cn.zjh.school.service.IClassesService;
import cn.zjh.school.domain.Classes;
import cn.zjh.school.query.ClassesQuery;
import cn.zjh.base.utils.AjaxResult;
import cn.zjh.base.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/classes")
public class ClassesController {
    @Autowired
    public IClassesService classesService;


    /**
     * 查询所有
     * @return
     */
    @GetMapping()
    public List<Classes> loadAll(){

        return classesService.loadAll();
    }


    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Classes loadById(@PathVariable("id")Long id)
    {
        return classesService.loadById(id);
    }


    /**
     * 删除对象信息
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            classesService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }


    /**
     * 新增/修改
     * @param classes  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Classes classes){
        try {
            if( classes.getId()!=null)
                classesService.update(classes);
            else
                classesService.save(classes);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }

    /**
     * @Title: batchDel
     * @Description: 批量删除
     * @Author: Mr.Zhang
     * @Version: 1.0
     * @Date:  2022/6/25 22:12
     * @Parameters:
     * @Return
     */
    @PatchMapping()
    public AjaxResult batchDel(@RequestBody Long[] ids){
        try {
            classesService.batchDel(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统错误，请重试！！！");
        }
    }

    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<Classes> queryData(@RequestBody ClassesQuery query)
    {
        return classesService.queryData(query);
    }




}
